package com.fc.mall.controller;

import com.fc.mall.dto.*;
import com.fc.mall.service.SmsBargainPromotionService;
import com.fc.mall.service.SmsBargainService;
import com.fc.mall.dto.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 砍价活动管理Controller
 * Created by dobuy on 2018/11/16.
 */
@Controller
@Api(tags = "SmsBargainPromotionController",description = "砍价活动管理")
@RequestMapping("/promotion/bargain")
public class SmsBargainPromotionController {
    @Autowired
    private SmsBargainPromotionService bargainPromotionService;

    @Autowired
    private SmsBargainService smsBargainService;

    @ApiOperation("添加砍价活动")
    @RequestMapping(value = "/create",method = RequestMethod.POST)
    @ResponseBody
    public Object create(@RequestBody SmsBargainPromotion bargainPromotion){
        int count = bargainPromotionService.create(bargainPromotion);
        if(count>0){
            return new CommonResult().success(count);
        }
        return new CommonResult().failed();
    }

    @ApiOperation("编辑砍价活动信息")
    @RequestMapping(value = "/update/{id}",method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable Long id, @RequestBody SmsBargainPromotion bargainPromotion){
        int count = bargainPromotionService.update(id,bargainPromotion);
        if(count>0){
            return new CommonResult().success(count);
        }
        return new CommonResult().failed();
    }

    @ApiOperation("删除砍价活动信息")
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.POST)
    @ResponseBody
    public Object delete(@PathVariable Long id){
        int count = bargainPromotionService.delete(id);
        if(count>0){
            return new CommonResult().success(count);
        }
        return new CommonResult().failed();
    }

    @ApiOperation("获取砍价活动详情")
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Object getItem(@PathVariable Long id){
        SmsBargainPromotion flashPromotion = bargainPromotionService.getBargainPromotion(id);
        return new CommonResult().success(flashPromotion);
    }

    @ApiOperation("根据砍价活动名称分页查询")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public Object getItem(@RequestParam(value = "keyword",required = false)String keyword,
                          @RequestParam(value = "filterType",required = false)Integer filterType,
                          @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                          @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum){
        List<SmsBargainPromotion> bargainPromotion = bargainPromotionService.list(keyword,filterType,pageSize,pageNum);
        return new CommonResult().pageSuccess(bargainPromotion);
    }

    @ApiOperation("根据砍价活动名称分页查询")
    @RequestMapping(value = "/all",method = RequestMethod.GET)
    @ResponseBody
    public Object getAllActiveItem(){
        List<SmsBargainPromotion> bargainPromotion = bargainPromotionService.getAllActive();
        return new CommonResult().success(bargainPromotion);
    }

    @ApiOperation("获取砍价活动产品列表")
    @RequestMapping(value = "/product/list/{pid}",method = RequestMethod.GET)
    @ResponseBody
    public Object getProducts(@PathVariable Long pid,
                              @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                              @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                              @RequestParam(value = "scope",defaultValue = "include")String scope,
                              @RequestParam(value = "keyword",defaultValue = "")String keyword){
        //获取映射表中的产品编号
        List<SmsBargainPromotionProduct> bargainPromotionProducts = bargainPromotionService.getProductList(pid);
        List<Long> bargainIds = new ArrayList<>();
        List<SmsBargainProductDto> bargainProducts = new ArrayList<>();

        if(bargainPromotionProducts != null && bargainPromotionProducts.size()>0){
            SmsBargainParam smsBargainParam = new SmsBargainParam();
            for(SmsBargainPromotionProduct bargain: bargainPromotionProducts ){
                bargainIds.add(bargain.getBargainId());
            }
            smsBargainParam.setIds(bargainIds);
            if(keyword != null && !"".equals(keyword)){
                smsBargainParam.setName(keyword);
            }
            if("include".equals(scope)){
                bargainProducts = smsBargainService.getBargainProductByIds(smsBargainParam,pageNum, pageSize);
            }else if("exclude".equals(scope)){
                bargainProducts = smsBargainService.getBargainProductExcludeIds(smsBargainParam,pageNum, pageSize);
            }else{
                return new CommonResult().validateFailed("参数scope错误");
            }
        }else{
            if("exclude".equals(scope)){
                SmsBargainParam smsBargainParam = new SmsBargainParam();
                smsBargainParam.setFilterType(1);
                if(keyword != null && !"".equals(keyword)){
                    smsBargainParam.setName(keyword);
                }
                bargainProducts = smsBargainService.queryProductBargainList(smsBargainParam, pageNum, pageSize);
            }
        }
        return new CommonResult().pageSuccess(bargainProducts);
    }

    @ApiOperation("砍价活动增加产品")
    @RequestMapping(value = "/product/add",method = RequestMethod.POST)
    @ResponseBody
    public Object addProduct(@RequestBody SmsBargainPromotionProduct bargainPromotionProduct){
        int count = bargainPromotionService.addProduct(bargainPromotionProduct);
        if(count>0){
            return new CommonResult().success(count);
        }
        return new CommonResult().failed();
    }

    @ApiOperation("砍价活动移除产品")
    @RequestMapping(value = "/product/remove/{id}",method = RequestMethod.POST)
    @ResponseBody
    public Object removeProduct(@PathVariable Long id){
        int count = bargainPromotionService.removeProduct(id);
        if(count>0){
            return new CommonResult().success(count);
        }
        return new CommonResult().failed();
    }

    @ApiOperation("砍价活动移除产品")
    @RequestMapping(value = "/product/remove/pid/{pid}/bid/{bid}",method = RequestMethod.POST)
    @ResponseBody
    public Object removeProductByPromotionIdAndBargainId(@PathVariable Long pid, @PathVariable Long bid){
        int count = bargainPromotionService.removeProduct(pid,bid);
        if(count>0){
            return new CommonResult().success(count);
        }
        return new CommonResult().failed();
    }
}
